﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TreeView_Select(object sender, EventArgs e)
    {
        string urlPath = "search.aspx?index=" + ((TreeView)sender).SelectedNode.Text;
        contentFrame.Attributes["src"] = urlPath;
        XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
        string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
        myXml.Load(path);//读取XML文件 
        string xpath = XpathTransfer(TreeView_Whole.SelectedNode.ValuePath);//将树的路径转为XPATH
        XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的
        if (currentNode.Equals(myXml.DocumentElement))//如果被选择节点为根节点，屏蔽删除、修改、上移、下移按钮
        {
            Btn_Add.Enabled = true;
            Btn_Del.Enabled = false;
            Btn_Modify.Enabled = false;
            Btn_Up.Enabled = false;
            Btn_Down.Enabled = false;
        }
        else if (TreeView_Whole.SelectedNode.Depth >= 3)//如果被选择节点的深度大于等于3层，屏蔽添加按钮
        {
            //Response.Write(TreeView_Whole.SelectedNode.Depth);
            Btn_Add.Enabled = false;
            Btn_Del.Enabled = true;
            Btn_Modify.Enabled = true;
            Btn_Up.Enabled = true;
            Btn_Down.Enabled = true;
        }
        else
        {
            Btn_Add.Enabled = true;
            Btn_Del.Enabled = true;
            Btn_Modify.Enabled = true;
            Btn_Up.Enabled = true;
            Btn_Down.Enabled = true;
        }

    }

    //将树路径转化为XPATH
    protected string XpathTransfer(string path)
    {
        string xpath = "";
        string[] names = path.Split('/');
        xpath += "/leadin";
        for (int i = 1; i < names.Length; i++)
        {
            xpath += "/node[@name='" + names[i] + "']";
        }
        return xpath;
    }

    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        if (TreeView_Whole.SelectedNode != null)
        {
            XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
            string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
            myXml.Load(path);//读取XML文件 
            string xpath = XpathTransfer(TreeView_Whole.SelectedNode.ValuePath);//将树的路径转为XPATH
            XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的
            XmlElement newNode = myXml.CreateElement("node");//创建新节点
            newNode.SetAttribute("name", TB_Input.Text);//为节点添加属性
            currentNode.AppendChild(newNode);//将新节点添加到当前节点
            myXml.Save(path);
            //XmlDataSourceTest.DataFile = "~/Leadin.xml";
            //TreeViewTest.DataSourceID = "XmlDataSourceTest";
            Response.Redirect(Request.Url.ToString()); //刷新页面
        }
    }

    protected void Btn_Modify_Click(object sender, EventArgs e)
    {
        if (TreeView_Whole.SelectedNode != null)
        {
            XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
            string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
            myXml.Load(path);//读取XML文件 
            string xpath = XpathTransfer(TreeView_Whole.SelectedNode.ValuePath);//将树的路径转为XPATH
            XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的节点
            currentNode.Attributes["name"].Value = TB_Input.Text;
            myXml.Save(path);//保存修改
            Response.Redirect(Request.Url.ToString()); //刷新页面
        }
    }
    protected void Btn_Del_Click(object sender, EventArgs e)
    {
        if (TreeView_Whole.SelectedNode != null)
        {
            XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
            string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
            myXml.Load(path);//读取XML文件 
            string xpath = XpathTransfer(TreeView_Whole.SelectedNode.ValuePath);//将树的路径转为XPATH
            XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的节点
            currentNode.ParentNode.RemoveChild(currentNode);//通过父节点移除自己
            myXml.Save(path);//保存修改
            Response.Redirect(Request.Url.ToString()); //刷新页面
        }
    }
    protected void Btn_Up_Click(object sender, EventArgs e)
    {
        if (TreeView_Whole.SelectedNode != null)
        {
            XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
            string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
            myXml.Load(path);//读取XML文件 
            string xpath = XpathTransfer(TreeView_Whole.SelectedNode.ValuePath);//将树的路径转为XPATH
            XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的
            if (currentNode.ParentNode.FirstChild.Equals(currentNode)) return;//如果节点已经是第一子节点则返回
            XmlNode tempNode = currentNode.PreviousSibling.Clone();//创建上一节点的副本
            currentNode.ParentNode.ReplaceChild(currentNode, currentNode.PreviousSibling);//先把上一节点替换为自己
            currentNode.ParentNode.InsertAfter(tempNode, currentNode);//把自己后面插入上一节点的副本
            myXml.Save(path);//保存修改
            Response.Redirect(Request.Url.ToString()); //刷新页面
        }
    }
    protected void Btn_Down_Click(object sender, EventArgs e)
    {
        if (TreeView_Whole.SelectedNode != null)
        {
            XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
            string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
            myXml.Load(path);//读取XML文件 
            string xpath = XpathTransfer(TreeView_Whole.SelectedNode.ValuePath);//将树的路径转为XPATH
            XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的节点
            if (currentNode.ParentNode.LastChild.Equals(currentNode)) return;//如果节点已经是最后子节点则返回
            XmlNode tempNode = currentNode.NextSibling.Clone();//创建下一节点的副本
            currentNode.ParentNode.ReplaceChild(currentNode, currentNode.NextSibling);//先把上一节点替换为自己
            currentNode.ParentNode.InsertBefore(tempNode, currentNode);//把自己后面插入上一节点的副本
            myXml.Save(path);//保存修改
            Response.Redirect(Request.Url.ToString()); //刷新页面
        }
    }
}
